package com.example.javabasic.other;

import com.alibaba.fastjson.JSON;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description 树结构
 * @Author Lin FuYuan
 * @date 2021/4/15
 */
public class Tree {
    public static void main(String[] args) {
        String x = "{\"desktop.http_port\":\"8000\",\"notebook.interpreters.sqlite.interface\":\"rdbms\",\"notebook.interpreters.oracle.interface\":\"rdbms\",\"notebook.interpreters.markdown.name\":\"Markdown\",\"desktop.time_zone\":\"America/Los_Angeles\",\"beeswax.hive_conf_dir\":\"\",\"liboozie.oozie_url\":\"http://172.24.115.19:11000/oozie\",\"desktop.database.engine\":\"mysql\",\"notebook.interpreters.solr\n" +
                ".name \":\" Solr SQL\",\"notebook.interpreters.pyspark.name\":\"PySpark\",\"notebook.interpreters.jar.interface\":\"livy-batch\",\"notebook.interpreters.pyspark.interface\":\"livy\",\"hadoop.hdfs_clusters.default.hadoop_hdfs_home\":\"/opt/install/hadoop\",\"notebook.interpreters.hive.name\":\"Hive\",\"notebook.interpreters.markdown.interface\":\"text\",\"desktop.smtp.tls\":\"no\",\"hadoop.hdfs_clusters.default.hadoop_bin\":\"/opt/install/hadoop/bin\",\"notebook.interpreters.solr.interface\":\"solr\",\"hadoop.yarn_clusters.default.resourcemanager_port\":\"8032\",\"desktop.database.password\":\"\",\"notebook.interpreters.spark.interface\":\"livy\",\"desktop.smtp.password\":\"\",\"notebook.interpreters.oracle.name \":\" Oracle\",\"desktop.database.user\":\"\",\"desktop.send_dbug_messages\":\"true\",\"desktop.http_host\":\"0.0.0.0\",\"notebook.interpreters.impala.name\":\"Impala\",\"hadoop.hdfs_clusters.default.fs_defaultfs\":\"hdfs://172.24.115.18:8020\",\"hadoop.hdfs_clusters.default.hadoop_conf_dir\":\"/opt/install/hadoop/etc/hadoop\",\"notebook.interpreters.postgresql.interface\":\"rdbms\",\"desktop.smtp.user\":\"\",\"hadoop.yarn_clusters.default.proxy_api_url\":\"http://172.24.115.18:8088\",\"notebook.interpreters.java.interface\":\"oozie\",\"beeswax.hive_server_host\":\"\",\"notebook.interpreters.mysql.interface\":\"rdbms\",\"hadoop.yarn_clusters.default.submit_to\":\"true\",\"desktop.secret_key\":\"jFE93j;2[290-eiw.KEiwN2s3['d;/.q[eIW^y#e=+Iei*@Mn<qW5o\",\"notebook.interpreters.sqlite.name \":\" SQLite\",\"notebook.interpreters.mysql.name \":\" MySQL\",\"hadoop.yarn_clusters.default.spark_history_server_url\":\"\",\"desktop.database.host\":\"\",\"notebook.interpreters.text.interface\":\"text\",\"notebook.interpreters.postgresql.name \":\" PostgreSQL\",\"notebook.interpreters.java.name\":\"Java\",\"hadoop.hdfs_clusters.default.webhdfs_url\":\"http://172.24.115.18:50070\",\"hadoop.yarn_clusters.default.resourcemanager_host\":\"172.24.115.18\",\"notebook.interpreters.spark.name\":\"Scala\",\"hadoop.yarn_clusters.default.history_server_api_url\":\"http://172.24.115.18:19888\",\"desktop.smtp.port\":\"25\",\"desktop.auth.idle_session_timeout\":\"-1\",\"notebook.interpreters.text.name\":\"Text\",\"hadoop.yarn_clusters.default.resourcemanager_api_url\":\"http://172.24.115.18:8088\",\"notebook.interpreters.pig.interface\":\"pig\",\"notebook.interpreters.py.name\":\"Spark Submit Python\",\"notebook.interpreters.jar.name\":\"Spark Submit Jar\",\"desktop.database.port\":\"3306\",\"desktop.database.name\":\"hue\",\"notebook.interpreters.impala.interface\":\"hiveserver2\",\"beeswax.hive_server_port\":\"\",\"notebook.interpreters.r.name\":\"R\",\"notebook.interpreters.r.interface\":\"livy\",\"desktop.smtp.host\":\"localhost\",\"notebook.interpreters.hive.interface\":\"hiveserver2\",\"notebook.interpreters.py.interface\":\"livy-batch\",\"notebook.interpreters.pig.name\":\"Pig\",\"hadoop.mapred_clusters.default.submit_to\":\"False\"}";

        HashMap<String,String> hashMap = JSON.parseObject(x, HashMap.class);
        List<String> list = new ArrayList<>();
        hashMap.forEach((k,v)->{
            list.add(k+"="+v);
        });
        List<String> collect = list.stream().sorted().collect(Collectors.toList());
        String s = JSON.toJSONString(collect);
        test(hashMap);

    }

    public static void test(Map<String,String> map) {
        Trie trie = new Trie();
        map.forEach((k,v)->{
            String[] split = k.trim().split("\\.");
            trie.insert(split,v);
        });
        Node node = trie.getNode();
        System.out.println(JSON.toJSONString(node));
        //遍历打印
        StringBuffer buffer = new StringBuffer();
        print(node,buffer);
        System.out.println(buffer.toString());





    }

    public static void print(Node node,StringBuffer result){
        StringBuffer buffer  = new StringBuffer();
        StringBuffer startBuffer = new StringBuffer();
        StringBuffer endBuffer  = new StringBuffer();
        startBuffer.append("[");
        endBuffer.append("]");
        for (Integer i = 1; i < node.getCengji(); i++) {
            buffer.append("  ");
            startBuffer.append("[");
            endBuffer.append("]");
        }

        List<Node> childs = node.getChilds();
        if(node.getCengji() > 0 ){
            if(CollectionUtils.isEmpty(childs)){
                result.append(buffer.toString()+node.getName()+"="+ node.getValue()+System.lineSeparator());
            }else{
                result.append(buffer.toString()+startBuffer.toString()+node.getName()+endBuffer.toString()+System.lineSeparator());
            }
        }
        List<Node> collect = childs.stream().sorted((a, b) -> {
            return b.getSort() - a.getSort();
        }).collect(Collectors.toList());
        for (Node child : collect) {
            print(child,result);
        }
    }


}
